package com.code.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;

import com.code.plugin.MessagesProperties;
import com.code.view.Activator;
import com.code.view.ViewUtils;


/**
 * 系统运行时日志记录和读取类
 *
 *
 * @author 朱国军
 * @version 2015年3月16日 朱国军
 */
public class ExceputionUtils {
    private static String logFile;


    /**
     * 保存日志信息
     *
     * @param exception 异常
     */
    public static void logging(Exception exception) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile();
        }

        PrintWriter printWriter;
        try {
            printWriter = new PrintWriter(new FileWriter(logFile, true));
            exception.printStackTrace(printWriter);
            printWriter.flush();
            printWriter.close();
        } catch (IOException e) {
            saveEclipseLog(e, MessagesProperties.log_save_log_exception);
        }
    }


    /**
     * 保存日志信息
     *
     * @param exception 异常
     * @param message 消息
     */
    public static void logging(String message, Exception exception) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile();
        }
        PrintWriter printWriter;
        try {
            printWriter = new PrintWriter(new FileWriter(logFile, true));
            printWriter.println(message + ":"); //$NON-NLS-1$
            exception.printStackTrace(printWriter);
            printWriter.flush();
            printWriter.close();
        } catch (IOException e) {
            saveEclipseLog(e, MessagesProperties.log_save_log_exception);
        }
    }


    /**
     * 获取首选项配置文件存放路径
     */
    public static void initLogFile() {
        logFile = Activator.getDataFolder() + "log.log"; //$NON-NLS-1$
        ViewUtils.createFolder(logFile, true);
    }


    /**
     * 清空日志文件
     *
     */
    public static void clearLogFile() {
        if (StringUtils.isBlank(logFile)) {
            initLogFile();
        }
        File file = new File(logFile);
        if (file.exists() && file.isFile()) {
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            saveEclipseLog(e, MessagesProperties.log_clean_log_exception);
        }
    }


    /**
     * 读取日志信息
     *
     * @param window 窗口
     *
     */
    public static void readLog(IWorkbenchWindow window) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile();
        }
        IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(logFile));
        if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
            IWorkbenchPage page = window.getActivePage();
            try {
                IDE.openEditorOnFileStore(page, fileStore);
            } catch (PartInitException e) {
                e.printStackTrace();
            }
        } else {
            String msgFmt = MessagesProperties.log_log_file_not_exist;
            String msg = MessageFormat.format(msgFmt, new Object[] { fileStore.getName() });
            MessageDialog.openWarning(window.getShell(), MessagesProperties.log_warn_title, msg);
        }
    }


    private static void saveEclipseLog(Exception e, String message) {
        IStatus status = new Status(Status.ERROR, Activator.PLUGIN_ID, message, e);
        Activator.getDefault().getLog().log(status);
    }

}
